<template>
  <el-dialog title="流程历史信息" :visible="visible" @close="close()">
    <el-table stripe header-cell-class-name="bindonce" :data="history" element-loading-text="列表正在加载中" border fit
      highlight-current-row>
      <el-table-column align="center" label="环节" width="150" prop="taskName">
      </el-table-column>
      <el-table-column align="center" label="委派人" prop="owner">
      </el-table-column>
      <el-table-column align="center" label="办理人" prop="assignee">
      </el-table-column>
      <el-table-column align="center" label="开始时间" width="180" prop="startTime" :formatter="formatDate">
      </el-table-column>
      <el-table-column align="center" label="结束时间" width="180" prop="endTime" :formatter="formatDate">
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click" class="margin-right-btn">
            <el-button type="text" size="mini" class="el-dropdown-link">评论</el-button>
            <el-dropdown-menu slot="dropdown" class="task-detail">
              <el-form style="min-width: 300px;margin: 5px;">
                <template v-if="scope.row.comments.length == 0">无</template>
                <template v-else>
                  <el-form-item v-for="(item,index) in scope.row.comments" :label="item.userId + '：'">
                    {{item.fullMessage}}
                  </el-form-item>
                </template>
              </el-form>
            </el-dropdown-menu>
            </el-button>
          </el-dropdown>
          <taskDetail :activityInstanceId="scope.row.activityInstanceId"></taskDetail>
          <el-button v-if="restorable" type="text" size="mini" @click="handleBack(scope.row)">退回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close()">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import access from '../api/access.js'
  import taskDetail from './taskDetail.vue'
  import utils from '../utils.js'

  export default {
    data() {
      return {
        history: []
      }
    },
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      restorable: {
        type: Boolean,
        default: false
      },
      processInstanceId: {
        type: String,
        default: null
      }
    },
    components: {
      taskDetail
    },
    watch: {
      visible(value, old) {
        if (value) {
          access.processHistory(this.processInstanceId).then(res => {
            this.history = res.data
          })
        }
      }
    },
    methods: {
      formatDate(row, column) {
        return utils.dateRenderer(row[column.property])
      },
      handleBack(row) {
        this.$confirm('退回任务后，该任务将退回到此节点，确定退回吗？', '提示').then(btn => {
          access.backTask(row.id, '测试退回').then(res => {
            if (!res.success) {
              this.$message.error(res.message || '退回失败')
            } else {
              this.$message.success('退回成功')
              this.loadTableData()
            }
          })
        }, btn => {})
      },
      close() {
        this.$emit('onClose')
      }
    }
  }
</script>

<style>
  .history-json-view .jsoneditor {
    min-height: 200px;
  }
</style>
